import type { RouteRecordRaw } from 'vue-router'

const dynamicRouteFiles = import.meta.glob('./modules/**/*.ts', {
  eager: true,
})

const moduleRoutes: RouteRecordRaw[] = []

for (const routeModule of Object.values(dynamicRouteFiles)) {
  const routes = (routeModule as { default: RouteRecordRaw[] }).default ?? []
  moduleRoutes.push(...routes)
}

const commonsRoutes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/LoginPage.vue'),
  },
  {
    path: '/codeCallback',
    name: 'codeCallback',
    component: () => import('@/views/common/CodeCallback.vue'),
    props: true,
  },
]

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/home',
  },

  ...moduleRoutes,
  ...commonsRoutes,
  {
    path: '/error',
    component: () => import('@/layouts/BaseLayout.vue'),
    children: [
      {
        path: '/:path(.*)*',
        props: { code: '404', message: '未找到页面' },
        component: () => import('@/views/common/CommonError.vue'),
      },
    ],
  },
]

export { commonsRoutes, moduleRoutes, routes }
